package cn.com.surker.base.controller;

import cn.com.surker.base.system.entity.SysControlStorage;
import cn.com.surker.common.AbstractController;
import cn.com.surker.dto.system.SysControlStorageDTO;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.system.service.SysControlStorageService;
import cn.com.surker.util.StringUtils;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.transaction.RollbackException;
import java.util.List;

/**
 * @Author lucky
 * @create 2024年10月31 9:43
 * @desc 工控仓位供应信息
 */
@RestController
@RequestMapping("/control/storage")
public class SysControlStorageController extends AbstractController {

    @Resource
    private SysControlStorageService controlStorageService;

    @PostMapping(value = "/find-all")
    @ApiOperation(value = "查询所有工控仓位信息", tags = "工控仓位")
    public RestResultDto findAll(){
        List<SysControlStorage> list = controlStorageService.findAll();
        if (list == null || list.size() == 0)
            return RestResultDto.fail("没有数据");
        return RestResultDto.success(list);
    }

    @PostMapping(value = "/page")
    @ApiOperation(value = "分页方法", tags = "工控仓位")
    public RestResultDto pageData(String searchVal, Long lineId, Integer page, Integer limit, Integer curr){
        try {
            /**分页对象**/
            if (!StringUtils.isNull(curr))//layui不能用page传页码数则修改默认页码参数传入
                page = curr;
            else {
                if (StringUtils.isNull(page))
                    page = 1;
            }
            if (StringUtils.isNull(page))
                page = 1;
            if (StringUtils.isNull(limit))
                limit = 15;
            IPage iPage = controlStorageService.findPage(searchVal, lineId, (page - 1) * limit, limit);
            return RestResultDto.success(iPage);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "新增方法", tags = "工控仓位")
    @Transactional(rollbackFor = RollbackException.class, propagation = Propagation.REQUIRED)
    public RestResultDto save(@RequestBody SysControlStorageDTO dto, BindingResult results){
        try {
            SysControlStorage controlStorage = new SysControlStorage();
            BeanUtil.copyProperties(dto,controlStorage);
            if (results.hasErrors()) {
                String defaultMessage = results.getFieldError().getDefaultMessage();
                return RestResultDto.fail(defaultMessage);
            }
            CacheUser user = getUser();
            controlStorage.setCreator(user.getName());

            return controlStorageService.saveControl(controlStorage);
        } catch (Exception e) {//多数据源手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新编辑方法", tags = "工控仓位")
    @Transactional(rollbackFor = RollbackException.class, propagation = Propagation.REQUIRED)
    public RestResultDto update(@RequestBody SysControlStorageDTO dto){
        try {
            SysControlStorage controlStorage = new SysControlStorage();
            BeanUtil.copyProperties(dto,controlStorage);
            CacheUser user = getUser();
            controlStorage.setCreator(user.getName());

            return controlStorageService.updateControl(controlStorage);
        } catch (Exception e) {//多数据源手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/update-status")
    @ApiOperation(value = "更新状态方法", tags = "工控仓位")
    @Transactional(rollbackFor = RollbackException.class, propagation = Propagation.REQUIRED)
    public RestResultDto updateStatus(Long id, Integer status, String remark){
        try {
            CacheUser user = getUser();

            return controlStorageService.updateStatus(id, status, user.getName(), remark);
        } catch (Exception e) {//多数据源手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }
}
